using System;
using System.Collections.Generic;
using System.Text;
using LeisureCenter.Api;
using LeisureCenter.Core;
using LeisureCenter.OptionsForms;
using LeisureCenter.BrowserItems;

namespace LeisureCenter.MenuItems
{
    public abstract class BaseLibrarySubmenuItem : IMenuItem
    {
        public BaseLibraryBrowserFolderItem lastFolder;
        protected BaseLibraryBrowserFolderItem rootFolder;
        protected long libraryId;

        protected abstract BaseLibraryBrowserFolderItem CreateRootFolder();

        protected BaseLibrarySubmenuItem(long libraryId)
        {
            this.libraryId = libraryId;
        }

        #region IMenuItem Membres

        public abstract string Text { get; }

        public abstract string IconName { get; }

        public void Execute()
        {
            if (lastFolder == null)
                ExecuteAgain();
            else
                Program.LeisureCenterInstance.BrowserManager.Browse(lastFolder);
        }

        public void ExecuteAgain()
        {
            rootFolder = CreateRootFolder();
            if (rootFolder != null)
                Program.LeisureCenterInstance.BrowserManager.Browse(rootFolder);
        }

        public void OptionsExecute()
        {
        }

        #endregion
    }

    public class LibraryAudioMediasSubmenuItem : BaseLibrarySubmenuItem
    {
        public LibraryAudioMediasSubmenuItem(long libraryId)
            : base (libraryId)
        {
        }

        public override string Text { get { return "Audio"; } }

        public override string IconName { get { return "music"; } }

        protected override BaseLibraryBrowserFolderItem CreateRootFolder()
        {
            return new LibraryAudioBrowserFolderItem(this.libraryId);
        }
    }

    public class LibraryVideoMediasSubmenuItem : BaseLibrarySubmenuItem
    {
        public LibraryVideoMediasSubmenuItem(long libraryId)
            : base(libraryId)
        {
        }

        public override string Text { get { return "Video"; } }

        public override string IconName { get { return "video"; } }

        protected override BaseLibraryBrowserFolderItem CreateRootFolder()
        {
            return new LibraryVideoBrowserFolderItem(this.libraryId);
        }
    }

    public class LibrarySelectorSubmenuItem : IMenuItem
    {
        private long libraryId;
        private LibraryMainMenuItem mainMenuItem;
        private string name;

        public LibrarySelectorSubmenuItem(LibraryMainMenuItem mainMenuItem, long libraryId, string name)
        {
            this.libraryId = libraryId;
            this.mainMenuItem = mainMenuItem;
            this.name = name;
        }

        #region IMenuItem Membres

        public string Text { get { return this.name; } }

        public string IconName { get { return @"library"; } }

        public void Execute()
        {
            ExecuteAgain();
        }

        public void ExecuteAgain()
        {
            this.mainMenuItem.libraryId = this.libraryId;
            this.mainMenuItem.ExecuteAgain();
        }

        public void OptionsExecute()
        {
            if (this.libraryId != 0)
            {
                LibraryOptionsDialog dialog = new LibraryOptionsDialog();
                using (dialog)
                {
                    dialog.LibraryId = this.libraryId;
                    if (dialog.Execute())
                        this.mainMenuItem.ExecuteAgain();
                }
            }
        }

        #endregion
    }

    public class LibraryMainMenuItem : IMenuItem
    {
        internal long libraryId = 1;

        private List<IMenuItem> items;

        #region IMenuItem Membres

        public string Text { get { return @"Library"; } }

        public string IconName { get { return @"library"; } }

        public void Execute()
        {
            if (items != null)
                Program.LeisureCenterInstance.MenuManager.FillSubmenu(items);
            else
                ExecuteAgain();
        }

        public void ExecuteAgain()
        {
            this.items = new List<IMenuItem>();

            this.items.Add(new LibraryAudioMediasSubmenuItem(this.libraryId));
            this.items.Add(new LibraryVideoMediasSubmenuItem(this.libraryId));

            IEnumerable<KeyValuePair<string, long>> libraries = DatabaseManager.ExecuteStringsLongsQuery("SELECT name, id FROM Libraries WHERE id <> " + this.libraryId);
            foreach (KeyValuePair<string, long> library in libraries)
            {
                this.items.Add(new LibrarySelectorSubmenuItem(this, library.Value, library.Key));
            }

            if (this.libraryId != 0)
                this.items.Add(new LibrarySelectorSubmenuItem(this, 0, "All"));

            Program.LeisureCenterInstance.MenuManager.FillSubmenu(items);
        }

        public void OptionsExecute()
        {
        }

        #endregion
    }

    public class LibraryMainMenuItemsProvider : IMainMenuItemsProvider
    {
        private List<IMenuItem> items = new List<IMenuItem>();

        public LibraryMainMenuItemsProvider()
        {
            items.Add(new LibraryMainMenuItem());
        }

        #region IMainMenuItemsProvider Membres

        public IEnumerable<IMenuItem> Items { get { return items; } }

        #endregion
    }
}
